package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.JournalEntry.AdmissionNumberSummaryJournal;
import com.gljx.web.Entity.User;
import com.gljx.web.service.AdmissionNumberSummaryJournaService;
import com.gljx.web.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author lcx
 * @Date 2017/12/27 0027 下午 1:47
 * 每日-招生人数\车型占比统计汇总表
 */
@Controller
@RequestMapping("/")
public class AdmissionNumberSummaryJournaController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private AdmissionNumberSummaryJournaService admissionNumberSummaryJournaService;

    /**
     * @Author lcx
     * @Date 2017/12/18 0018 上午 9:42
     * 查询加油站进销存统计报表
     */
    @RequestMapping(value = "/chooseAdmissionNumberSummaryJournal/look.action")
    public String chooseAdmissionNumberSummaryJournal(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");                                   //获取已经登录用户数据
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
        Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
        cale.add(Calendar.MONTH, -1);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        Date resultDate = cale.getTime(); // 结果
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String beginTime = sdf.format(resultDate);

        request.setAttribute("company", department);
        request.setAttribute("beginTime", beginTime);

        return "backHome/journaling/manageJournaling/AdmissionNumberSummaryJournaling/AdmissionNumberSummaryJournalingList";

    }

    @RequestMapping(value = "/selAdmissionNumberSummaryJournal")
    @ResponseBody
    @Transactional
    public Map selAdmissionNumberSummaryJournal(HttpServletRequest request, @RequestParam(defaultValue = "", value = "cfoilspecName") String cfoilspecName,
                                                @RequestParam(defaultValue = "", value = "begindate") String begindate,
                                                @RequestParam(defaultValue = "", value = "begindate2") String begindate2,
                                                @RequestParam(defaultValue = "", value = "enddate") String enddate,
                                                @RequestParam(defaultValue = "", value = "state") String state) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");                                   //获取已经登录用户数据
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
        //1.查询
        Map returnmap = new HashMap();

        Map map = new HashMap();
        map.put("companyId", department.getFid());

        //查询这个月的 部门日招生完成情况统计表
        if ("0".equals(state)) {
            map.put("beginTime", begindate);

            List<AdmissionNumberSummaryJournal> admissionNumberSummaryJournals = admissionNumberSummaryJournaService.selectSummartAsDay(map);

            returnmap.put("admissionNumberSummaryJournals", admissionNumberSummaryJournals);
        }
        if ("1".equals(state)) {
            map.put("beginTime", begindate2);

            List<AdmissionNumberSummaryJournal> admissionNumberSummaryJournals = admissionNumberSummaryJournaService.selectSummartAsMonth(map);

            returnmap.put("admissionNumberSummaryJournals", admissionNumberSummaryJournals);
        }
        returnmap.put("state", state);
        returnmap.put("gsName", department.getFnameL2());

        return returnmap;
    }

}
